package com.axend.lib_base.utils

import android.animation.ObjectAnimator
import android.animation.TypeEvaluator
import android.animation.ValueAnimator
import android.graphics.Path
import android.graphics.PathMeasure
import android.view.View
import android.view.ViewTreeObserver
import android.view.animation.LinearInterpolator

/**
 * 动画工具类
 */
object AnimationUtils {

    private var animator: ValueAnimator? = null
    private var animator360: ObjectAnimator? = null


    /**
     * 开始动画方法
     * @param view1 需要做圆圈动画的视图
     * @param view2 view2视图
     */
    fun startCircleAnimation(view1: View, view2: View) {
        // 使用 ViewTreeObserver 监听布局完成事件
        view2.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                // 移除监听器，避免重复调用
                view2.viewTreeObserver.removeOnGlobalLayoutListener(this)
                view1.visibility = View.VISIBLE

                // 获取蓝牙圆圈的中心点和半径
                val circleRadius = view2.width / 2
                val centerX = view2.left + circleRadius
                val centerY = view2.top + circleRadius

                // 创建 Path 对象，定义一个圆形路径
                val path = Path()
                path.addCircle(
                    centerX.toFloat(),
                    centerY.toFloat(),
                    (circleRadius - 5).toFloat(),
                    Path.Direction.CW
                )

                // 使用 PathMeasure 获取路径长度
                val pathMeasure = PathMeasure(path, false)
                val pathLength = pathMeasure.length

                // 创建 ValueAnimator，沿着路径移动
                animator = ValueAnimator.ofFloat(0f, pathLength)
                animator?.let {
                    it.duration = 5000 // 动画持续时间
                    it.repeatCount = ValueAnimator.INFINITE // 循环播放
                    it.interpolator = LinearInterpolator() // 线性插值器，保持匀速运动
                    val point = FloatArray(2)

                    // 添加更新监听器
                    it.addUpdateListener { valueAnimator ->
                        val fraction = valueAnimator.animatedValue as Float
                        pathMeasure.getPosTan(fraction, point, null)
                        view1.translationX = point[0] - view1.width / 2f
                        view1.translationY = point[1] - view1.height / 2f
                    }

                    // 启动动画
                    it.start()
                }
            }
        })
    }

    /**
     * 开始360度动画
     */
    fun start360Animation(view: View) {
        view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                view.viewTreeObserver.removeOnGlobalLayoutListener(this)
                // 设置旋转中心点为视图的中心
                view.pivotX = view.width / 2f
                view.pivotY = view.height / 2f
                // 创建 ObjectAnimator 实现旋转动画
                animator360 = ObjectAnimator.ofFloat(view, "rotation", 0f, 360f)
                animator360?.apply {
                    duration = 5000 // 动画持续时间，单位为毫秒
                    repeatCount = ValueAnimator.INFINITE // 循环播放
                    interpolator = LinearInterpolator() // 线性插值器，保持匀速运动

                    // 启动动画
                    start()
                }
            }

        })

    }


    /**
     * 结束动画方法
     */
    fun endCircleAnimation(view1: View) {
        view1.visibility = View.GONE
        animator?.cancel()
        animator = null
    }

    fun end360Animation() {
        animator360?.cancel()
        animator360 = null
    }

    // 自定义 PathEvaluator 类
    class PathEvaluator(private val path: Path) : TypeEvaluator<FloatArray> {

        private val point = FloatArray(2)

        override fun evaluate(
            fraction: Float,
            startValue: FloatArray?,
            endValue: FloatArray?
        ): FloatArray {
            val pathMeasure = PathMeasure(path, false)
            val distance = fraction * pathMeasure.length
            pathMeasure.getPosTan(distance, point, null)
            return point
        }
    }
}